package com.spdd.mt.biz.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * SPDD接口请求配置类
 */
@Data
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "channel.spdd")
public class SpddMapping {

    private Map<String, AppInfo> mapping;

    @Data
    public static class AppInfo {

        /** SPDD 应用ID */
        private String appId;

        /** SPDD 应用Key */
        private String appKey;

        /** 美团提供的供应商ID */
        private String partnerId;

        /** 美团提供的供应商Key,用于签名 */
        private String partnerKey;
    }

    /**
     * 根据 appId 获取 key
     *
     * @param mapping 商户号集合
     * @param appId   sppId
     */
    public static String getKeyByAppId(Map<String, AppInfo> mapping, String appId) {
        for (Map.Entry<String, AppInfo> entry : mapping.entrySet()) {
            if (entry.getValue().getAppId().equals(appId)) {
                return entry.getKey();
            }
        }
        return null;
    }

}
